﻿Imports System.IO
Imports System.Windows.Forms
Imports System.Text.RegularExpressions

Namespace Project
    Public Class ProjectFileTree

        Private _filetree As TreeView
        Private _rootdir As String
        Private _projectname As String

        ReadOnly Property FileTree As TreeView
            Get
                Return _filetree
            End Get
        End Property

        Public Sub New(ByRef projectname As String, ByRef fileview As TreeView, ByRef folder As String, Optional ByRef regular_match As String = "")
            _filetree = fileview
            _projectname = projectname

            ScanDirectory(folder, regular_match)
        End Sub

        Public Sub ScanDirectory(ByRef folder As String, Optional ByRef regular_match As String = "", Optional ByRef lastnode As TreeNode = Nothing)

            Dim re As New Regex(regular_match)

            Dim dir As New DirectoryInfo(folder)
            If IsNothing(lastnode) Then
                lastnode = New TreeNode(dir.Name)
                _filetree.Nodes.Add(lastnode)
                _filetree.TopNode = lastnode
            Else
                ' Ignore the Classes folders
                lastnode = lastnode.Nodes.Add(dir.Name)
            End If

            If lastnode.Text = "Src" Then
                lastnode.Name = _projectname
                lastnode.Text = _projectname
                lastnode.ImageIndex = 0
            Else
                lastnode.ImageIndex = 1
            End If

            lastnode.SelectedImageIndex = lastnode.ImageIndex

            For Each fileinfo In dir.GetFiles '("*.*", SearchOption.AllDirectories)
                ' Check if filename matches regex
                If re.IsMatch(fileinfo.Name) Then
                    ' Add file to list
                    Dim actor_file As New ProjectFile(fileinfo.FullName)

                    lastnode.Nodes.Add(actor_file.FileName, actor_file.FileName, 2, 2)
                End If
            Next

            For Each dir In dir.GetDirectories()
                ScanDirectory(dir.FullName, regular_match, lastnode)
                lastnode = lastnode.Parent
            Next
        End Sub

        ' Expand the main project folder and the "Classes" folders
        Public Sub ExpandDefaultFolders()
            ' Collapse everything first
            _filetree.Nodes(0).Collapse()

            ' Expand the Root Project Node
            _filetree.Nodes(0).Expand()

            For Each node As TreeNode In _filetree.Nodes
                For Each secondnode As TreeNode In node.Nodes
                    For Each thirdnode As TreeNode In secondnode.Nodes
                        ' Expand 
                        If thirdnode.Text.ToLower() = "classes" Then
                            thirdnode.Expand()
                        End If
                    Next
                Next
            Next
        End Sub

    End Class

End Namespace